package org.glut.wechat.moments.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glut.wechat.moments.domain.Moments;
import org.glut.wechat.moments.domain.MomentsLikes;
import org.glut.wechat.moments.domain.MomentsNotice;
import org.glut.wechat.moments.mapper.MomentsLikesMapper;
import org.glut.wechat.moments.mapper.MomentsMapper;
import org.glut.wechat.moments.mapper.MomentsNoticeMapper;
import org.glut.wechat.moments.service.MomentsLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 朋友圈点赞服务实现类
 */
@Service
public class MomentsLikesServiceImpl extends ServiceImpl<MomentsLikesMapper, MomentsLikes> implements MomentsLikesService {

    @Autowired
    private MomentsNoticeMapper momentsNoticeMapper;
    @Autowired
    private MomentsMapper momentsMapper;


    @Transactional
    @Override
    public void likeMoment(Integer momentId, Integer userId) {
        MomentsLikes like = new MomentsLikes();
        like.setMomentId(momentId);
        like.setUserId(userId);
        like.setCreateTime(LocalDateTime.now());
        baseMapper.insert(like);
        Moments moments = momentsMapper.selectById(momentId);
        LambdaQueryWrapper<MomentsNotice> momentsNoticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        momentsNoticeLambdaQueryWrapper.eq(MomentsNotice::getMomentId, momentId);
        momentsNoticeLambdaQueryWrapper.eq(MomentsNotice::getOperationUserId, userId);
        momentsNoticeLambdaQueryWrapper.eq(MomentsNotice::getNoticeUserId, moments.getUserId());
        momentsNoticeLambdaQueryWrapper.eq(MomentsNotice::getType, 0);
        MomentsNotice hasMomentsNotice = momentsNoticeMapper.selectOne(momentsNoticeLambdaQueryWrapper);
        if(!moments.getUserId().equals(userId) && hasMomentsNotice == null){
            MomentsNotice momentsNotice = new MomentsNotice();
            momentsNotice.setCreateTime(LocalDateTime.now());
            momentsNotice.setMomentId(userId);
            momentsNotice.setOperationUserId(userId);
            momentsNotice.setMomentId(moments.getId());
            momentsNotice.setOperationUserId(userId);
            momentsNotice.setNoticeUserId(moments.getUserId());
            momentsNotice.setType(0);
            momentsNoticeMapper.insert(momentsNotice);
        }

    }

    @Override
    public void unlikeMoment(Integer momentId, Integer userId) {
        QueryWrapper<MomentsLikes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("moment_id", momentId).eq("user_id", userId);
        baseMapper.delete(queryWrapper);
    }


}
